package com.yonyou.pmclouds.inspectitem.process;

import com.yonyou.pmclouds.basecom.bp.processor.MultiProcessor;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.inspectitem.entity.InspectItemVO;
import com.yonyou.pmclouds.measuredata.service.rmiitf.MeasureDataService;
import com.yonyou.pmclouds.problemlog.entity.ProblemLogVO;
import com.yonyou.pmclouds.problemlog.service.rmiitf.ProblemLogMaintain;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 检查项新增完成后，判断是否推送生成问题记录单
 * @author fangpengc
 *
 */
@Component
public class InspectItemAddAfterProcess implements MultiProcessor<InspectItemVO>{
    @Autowired
    private ProblemLogMaintain problemService;
    @Autowired
    private MeasureDataService measureService;
    @Override
    public void process(InspectItemVO[] vos, InspectItemVO[] origs) throws BusinessException {

        if(ArrayUtils.isEmpty(vos)) {
            return ;
        }
        List<ProblemLogVO> list = new ArrayList<>();
       for (InspectItemVO inspectItemVO : vos) {
           // 保存测量数据
           if(!ArrayUtils.isEmpty(inspectItemVO.getDatas())) {
               measureService.add(inspectItemVO.getDatas(), inspectItemVO.getPk_inspectitem());
           }
           if(inspectItemVO.isIspush()) {
               ProblemLogVO problemLogVO = transVO(inspectItemVO);
               list.add(problemLogVO);
           }
       }
       //推送生成问题记录
        if (list.size() > 0) {
           problemService.add(list.toArray(new ProblemLogVO[list.size()]));
       }

        
    }
    /**
     * 将检查项转化为问题记录
     * @param itemVO
     * @return
     * @throws BusinessException
     */
    private ProblemLogVO transVO(InspectItemVO itemVO) throws BusinessException {
        ProblemLogVO problemVO = new ProblemLogVO();
        problemVO.setPk_problemreason(itemVO.getPk_problemreason());
        problemVO.setPk_process(itemVO.getPk_process());
        problemVO.setPictures(itemVO.getPictures());
        problemVO.setPk_rectificationer(itemVO.getPk_rectificationer());
        problemVO.setRectificationer_name(itemVO.getRectificationer_name());
        problemVO.setPk_project(itemVO.getPk_project());
        problemVO.setContent(itemVO.getProblemContent());

        /**
         * 新增现场检查和检查项的主键，租户主键
         */
        problemVO.setPk_siteinspection(itemVO.getPk_bill()); //现场检查主键
        problemVO.setPk_inspectitem(itemVO.getPk_inspectitem()); //检查项主键
        problemVO.setEnddate(itemVO.getEnddate());  //完成期限
        problemVO.setPk_tenant(itemVO.getPk_tenant());  //租户

        problemVO.setPosition(itemVO.getPosition());
        problemVO.setLatitude(itemVO.getLatitude());
        problemVO.setLongitude(itemVO.getLongitude());
        problemVO.setCreator(itemVO.getCreator());
        problemVO.setCreator_name(itemVO.getCreatorName());
        problemVO.setCopyPersonVOS(itemVO.getCopyPersonVOS());
        problemVO.setOperation(ProblemLogVO.OPERATION_ADD);

        //新增 整改区域
        problemVO.setImprovement_area(itemVO.getImprovement_area());

        return problemVO;
    }
  

}
